package com.jishaokang.service.impl;

import com.jishaokang.base.Result;
import com.jishaokang.cache.ResultCache;
import com.jishaokang.dao.LiveDAO;
import com.jishaokang.dao.TeacherDAO;
import com.jishaokang.model.dto.Course;
import com.jishaokang.model.dto.Live;
import com.jishaokang.model.dto.User;
import com.jishaokang.model.vo.LiveVO;
import com.jishaokang.model.vo.UserVO;
import com.jishaokang.service.LiveService;
import com.jishaokang.util.LiveUtil;
import com.jishaokang.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by NANA_Final on 2019/6/21.
 */
@Service
public class LiveServiceImpl implements LiveService {

    @Autowired
    private LiveDAO liveDAO;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private TeacherDAO teacherDAO;

    @Override
    public Result selectLive(Live live) {
        int total = liveDAO.selectTotal(live);
        live.setPage((live.getPage()-1)*live.getNumber());
        List<LiveVO> lives = liveDAO.select(live);
        return ResultCache.getListDataOk(total,"lives",lives);
    }

    @Override
    public Result addLive(Live live) throws IOException {
        int userId = tokenUtil.getUserIdByToken(live.getToken());
        int teacherId = teacherDAO.selectByUserId(userId).getTeacherId();
        live.setTeacherId(teacherId);
        LiveVO liveVOs = liveDAO.selectByTeacherId(live);
        System.out.println("************************"+liveVOs);
        if (liveVOs != null) return ResultCache.getDataOk(liveVOs);
        liveDAO.insert(live);
        System.out.println(live);

        LiveVO liveVO = liveDAO.selectByLiveId(live);
        System.out.println(liveVO);
        live.setLiveUrl("http://182.92.1.116:3000/watchlive?liveId="+liveVO.getLiveId());
        live.setLiveId(liveVO.getLiveId());
        liveDAO.updateUrl(live);
        return ResultCache.OK;
    }

    @Override
    public Result deleteLiveByLiveId(Live live) throws IOException {
        liveDAO.delete(live.getLiveId());
        return ResultCache.OK;
    }

    @Override
    public Result deleteLiveByUserId(Live live) {
        int userId = tokenUtil.getUserIdByToken(live.getToken());
        int teacherId = teacherDAO.selectByUserId(userId).getTeacherId();
        liveDAO.deleteByTeacherId(teacherId);
        return ResultCache.OK;
    }

    @Override
    public Result selectLiveByLiveId(Live live) {
        LiveVO liveVO = liveDAO.selectByLiveId(live);
        return ResultCache.getDataOk(liveVO);
    }

    public static final String KEYSTRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    public static final int len = KEYSTRING.length();

    @Override
    public Result uploadLiveImage(String token, Integer liveId, MultipartFile imageFile, HttpServletRequest request) throws IOException {

        Random random = new Random();
        StringBuilder sb = new StringBuilder(new Date().getTime()+"");
        for (int i = 0;i < 50;i++ )
            sb = sb.append(KEYSTRING.charAt(random.nextInt(len)));
        sb.append(".");
        sb.append(imageFile.getOriginalFilename().split("\\.")[1]);
        String path = request.getServletContext().getRealPath("/upload/image/");
        String fileName = sb.toString();
        File targetFile = new File(path, fileName);
        System.out.println(path+"   "+fileName);
        targetFile.createNewFile();
        try {
            imageFile.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Live live = new Live();
        live.setLiveImage("upload/image/"+fileName);
        live.setLiveId(liveId);
        liveDAO.updateLiveImage(live);
        return ResultCache.OK;
    }

    @Override
    public Result updateWatchTotal(Live live) {
        liveDAO.updateWatchTotal(live);
        return ResultCache.OK;
    }

}